﻿# 从误差统计excel绘制出误差直方图
import numpy as np
import pandas as pd
import sys
import matplotlib.pyplot as plt

# plt.rcParams['font.sans-serif'] = ['Simhei']  # 用来正常显示中文标签
plt.rcParams['font.sans-serif'] = ['Simsun']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
plt.rcParams['figure.figsize'] = [28, 10]
plt.rcParams.update(
    {
        'text.usetex': False,
        # 'font.family': 'stixgeneral',
        'mathtext.fontset': 'stix',
        "font.size": 28
    }
)

data = 0
if sys.argv.__len__() == 1:
    path = input("输入csv路径")
    # path = r"C:\Users\17616\Desktop\本科毕设\实验数据记录\2021-5-20\G0_0-F4-0520\G0_0-LD4I2\histogram.csv"
    data = pd.read_csv(path, sep=',', encoding='gb2312')
else:
    data = pd.read_csv(sys.argv[1], sep=',', encoding='gb2312')

# 计算方差
meanSquareError = 0
meanError = 0
weightsSum = 0
for i, val in enumerate(data['误差']):
    meanError += val * data['数量'][i]
    weightsSum += data['数量'][i]
meanError /= weightsSum
for i, val in enumerate(data['误差']):
    meanSquareError += ((val - meanError) ** 2) * data['数量'][i]
meanSquareError /= weightsSum
meanSquareError = np.sqrt(meanSquareError)

# plt.figure(figsize=(20, 10))
plt.suptitle(u"复原图与原图误差分布直方图(误差中心%.3f，标准差%.3f)" % (meanError, meanSquareError))

plt.subplot(1, 2, 1)
plt.bar(data['误差'], data['数量'], width=1)
plt.xlabel("灰度值误差(LSB)")
plt.ylabel("像素个数(个)")
plt.yscale("log")
plt.subplot(1, 2, 2)
plt.bar(data['误差'], data['数量'], width=1)
plt.xlabel("灰度值误差(LSB)")
plt.ylabel("像素个数(个)")

plt.show()
